import Taro from '@tarojs/taro'
import app from '../../../../main.js'
let myJs = {
    getData: function() {
        this.state = {
            label_patients: {
                offset: 0,
                length: 25,
                items: [{
                    id: '',
                    patient_id: '',
                    patients_id_from_patient_id_name: '',
                    patients_id_from_patient_id_head_image: '',
                }]
            },
            label_patient: {
                id: '',
                patient_id: '',
                patients_id_from_patient_id_name: '',
                patients_id_from_patient_id_head_image: '',
            },
            label_name:''
        };
        return this.state;
    },
    allLabelPatient() {
        var me = this;
        var data = {};
        var flag = true;
        if(me.beforeAllLabelPatient) {
            flag = me.beforeAllLabelPatient(data);
        }
        if(flag) {
            app.get("api/label_patients/all", data, function(res) {
                var data = [];
                for(var i = 0; i < res.data.result.data.length; i++) {
                    var m = {
                        id: res.data.result.data[i].id,
                        patient_id: res.data.result.data[i].patient_id,
                        patients_id_from_patient_id_name: res.data.result.data[i].patients_id_from_patient_id_name,
                        patients_id_from_patient_id_head_image: res.data.result.data[i].patients_id_from_patient_id_head_image,
                    }
                    data.push(m);
                }
                me.setState({label_patients: {items: data}});
                if(me.afterAllLabelPatient) {
                    me.afterAllLabelPatient(res);
                }
            });
        }
    },
    createLabelPatient() {
        var me = this;
        var data = me.state.label_patient;
        var flag = true;
        if(me.beforeCreateLabelPatient) {
            flag = me.beforeCreateLabelPatient(data);
        }
        if(flag) {
            app.post("api/label_patients/create", data, function(res) {
                if(me.afterCreateLabelPatient) {
                    me.afterCreateLabelPatient(res);
                }
            });
        }
    },
    deleteLabelPatient() {
        var me = this;
        var data = {id: me.state.label_patient.id};
        var flag = true;
        if(me.beforeDeleteLabelPatient) {
            flag = me.beforeDeleteLabelPatient(data);
        }
        if(flag) {
            app.post("api/label_patients/delete", data, function(res) {
                if(me.afterDeleteLabelPatient) {
                    me.afterDeleteLabelPatient(res);
                }
            });
        }
    },
    navigateTo(url) {
        app.navigateTo(url);
    },
    handleChange(name, v) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        temp[names[i]] = v;
        this.setState({n: temp[names[i]]});
    },
    handleTextareaChange(name, e) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        temp[names[i]] = e.target.value;
        this.setState({n: temp[names[i]]});
    },
    redirectTo(url) {
        app.redirectTo(url);
    },
    componentDidMount () {},
    componentWillUnmount () {},
    componentDidHide () {}
}
export default myJs;
